Ext.define('Fxt.sys-config.Controller', {
    extend: 'Fxt.core.Controller',
    views: ['Fxt.sys-config.view.Viewport', 'Fxt.sys-config.view.ConfigForm'],
    $onInit: function() {
        var me = this;
        this.control({
            'button[action=save]': {
                click: function() {
                    var $viewport = me.$getViewport();
                    var $form = $viewport.down('form');
                    me.$doSaveConfig($form);
                }
            },
            'button[action=refresh]': {
                click: function() {
                    me.$refreshConfigs();
                }
            },
            'button[action=shutdown]': {
                click: function() {
                    me.$doShutdown();
                }
            }
        });
    },
    $refreshConfigs: function() {
        var me = this;
        Fxt.ajax({
            url: '/sys-config',
            method: 'GET',
            success: function(data) {
                var $viewport = me.$getViewport();
                var $form = $viewport.down('form');
                $form.getForm().setValues({
                    'configs': data.str
                });
            }
        });
    },
    $doInit: function() {
        this.$refreshConfigs();
    },
    $doSaveConfig: function(form) {
        var me = this;
    	if (!form.isValid()) {
    	    return;
    	}
        form.getForm().submit({
    	    url: Fxt.path + '/sys-config/save',
    	    waitMsg: '正在保存配置',
    	    success: function() {
                Fxt.success('保存成功');
                me.$refreshConfigs();
    	    },
    	    failure: Fxt._defaultSubmitFailure
    	});
    },
    $doShutdown: function() {
        Ext.Msg.confirm('提示', '确定要关闭程序么？', function(ok) {
            if (ok != 'yes') {
                return;
            }
            Fxt.ajax({
                url: '/sys-config/shutdown',
                method: 'POST',
                success: function() {

                }
            });
        });
    }
});